<?php
namespace app\admin\controller;
use think\Db;

class Carousel
{
	public function __construct(){
		Admin_auth();
	}
	
    public function index()
    {
		$cid = input('get.cid');
		if(!empty($cid)){
			$where = ['cid'=>$cid];
		}else{
			$where = "";
		}
		$pages = pages('carousel',$where,input('get.page'));
		$data = Db::table("carousel")->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
        return view('',['data'  => $data,'pages' => $pages['pages']]);
    }
	
	public function edit()
    {
		$id = input("get.id")?input("get.id"):0;
		if(request()->isPost()){
			$data = request()->post();
			if(!$data['aid']){
				echo '请选择城市';
			}else{
				$data['cid'] = Db::table('area')->where(['id' => $data['aid']])->value("father_id");
				$data['pid'] = Db::table('city')->where(['id' => $data['cid']])->value("father_id");
				if($data['url_type'] == "商家"){
					$data['url'] = $data['url_seller'];
				}
				unset($data['url_seller']);
				if($id){
					Db::table("carousel")->whereIn('aid', session('aid'))->where(['id' => $id])->update($data);
				}else{
					$data['time'] = time();
					$id = Db::table("carousel")->insertGetId($data);
				}
			}
		}
		$data = Db::table("carousel")->whereIn('aid', session('aid'))->where(['id' => $id])->find();
		if($data['aid']){
			$where = array(
				'aid' => $data['aid']
			);
		}
		$seller_list = Db::table("seller")->whereIn('aid',session('aid'))->where($where)->select();
        return view('',['data' => $data,'seller_list' => $seller_list]);
    }
   
    public function del()
    {
		$id = input("get.id")?input("get.id"):0;
		$page = input("get.page");
		$data = Db::table("carousel")->whereIn('aid',session('aid'))->delete($id);
		redirect(url('/admin/carousel').'?page='.$page);
		exit;
    }
}
